﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Inaction.ServiceProxy
{
    [Serializable]
    public class RequestData
    {
        public string Assembly { get; set; }

        public string FullName { get; set; }

        [NonSerialized]
        Type type = null;

        public Type EntityType
        {
            get
            {
                if (type != null) return type;
                if (string.IsNullOrEmpty(Assembly) || string.IsNullOrEmpty(FullName)) return null;
                type = TypeCache.Get(Assembly, FullName);
                if (type == null)
                {
                    type = System.Reflection.Assembly.LoadFile(Inaction.Util.AppHelper.ExecuteDirectory + Assembly + ".dll").GetType(FullName);
                    TypeCache.Add(Assembly, FullName, type);
                }
                return type;
            }
        }

        public object Data { get; set; }

        public Inaction.Security.IIdentity Identity { get; private set; }

        public RequestData()
        {
            if (Inaction.Security.Principal.Instance != null)
            {
                this.Identity = Inaction.Security.Principal.Instance.Identity;
            }
        }

        public RequestData(object data, string assembly = null, string fullName = null)
            : this()
        {
            this.Data = data;
            this.Assembly = assembly;
            this.FullName = fullName;
        }
    }
}
